<?php

namespace app\common\controller;

use app\common\library\basicadmin\logic\Delete;
use app\common\library\basicadmin\logic\Export;
use app\common\library\basicadmin\logic\Form;
use app\common\library\basicadmin\logic\Page;
use app\common\library\basicadmin\logic\Query;
use app\common\library\basicadmin\logic\Save;
use library\Controller;
use think\Db;
use think\facade\Lang;
use think\Model;

/**
 * @method  Query _query($table)
 * @method array _input($data, $rule = [], $info = [])
 * @method Delete _delete(Model $model, $pkField = '', $where = [])
 * @method Save _save(Model $model, $data = [], $pkField = '', $where = [])
 * @method Form _form(Model $model, $tplFile = '', $pkField = '', $where = [], $data = [])
 * @method Page _page(Model $model, $isPage = true, $isDisplay = true, $total = false, $limit = 0)
 * @method Export _export($data, $table_field)
 * --------------------------------
 * 重构library\Controller,使用模型的方式来操作数据库
 * Class BasicAdmin
 * @package app\common\controller
 */
class BasicAdmin extends Controller
{
    protected $table = '';
    /**
     * @var null|Model
     */
    protected $model = null;
    public $resultset_type = 'array'; //select 查询出来的数据类型 array,collection

    public function __construct()
    {
        Lang::load(   __DIR__ . '/../lang/zh-cn.php');
        parent::__construct();
        if ($this->table && empty($this->model)){
            $this->model = model($this->table);
        } else if (is_string($this->model)) {
            $this->model = model($this->model);
        }
        $this->base_route = rtrim(url('',false,false), $this->request->action());
    }

    /**
     * @param string $method
     * @param array $arguments
     * @return mixed
     * @throws \ReflectionException
     * @throws \think\Exception
     */
    public function __call($method, $arguments = [])
    {
        if (class_exists($name = "\\app\\common\\library\\basicadmin\\logic\\" . ucfirst(ltrim($method, '_')))) {
            return (new \ReflectionClass($name))->newInstanceArgs($arguments)->init($this);
        }
        throw new \think\Exception('method not exists:' . get_class($this) . '->' . $method);
    }


}